/*******************************************************************************
Replication Materials for Blau, Kahn, Brummund, Cook, and Larson-Koester "Is 
There Still Son Preference in the United States?"

Table R8, Age at First Birth Control (Table 5 Fertility Regressions)

Date Modified: 10/19/2019

*******************************************************************************/

*Define needed variables
local v "secgen m_i* f_i* own_imm* sp_imm* m_bpld f_bpld core_samp core_sampf citizen"
local v "`v' hhwtnorm lths scol cold sp_lths sp_scol sp_cold genrace sp_genrace"
local v "`v' region year age* sp_age* chld1 gt0chld femhdalt incountry head_samp nchild c_age1"

*Load data
use `v' using "$cps\regfile_March_core", clear

*Keep only second generation immigrants that are the household head or spouse of household head
keep if head_samp == 1
keep if secgen==1

*Drop observations with parents from countries without characteristics
drop if incountry == 0 

*Gen Mother Age at First Birth
gen age_fb = .
replace age_fb = age - c_age1 if c_age1!=.

/*******************************************************************************

I. Variable Cleaning and Generation

*******************************************************************************/

*Clean up some source country variables
	foreach mm in "m" "f" {
	
		*Log Transform GDP
		gen `mm'_ligdp00_07 = log(`mm'_igdp00_07)
		replace `mm'_igdp00_07 = `mm'_igdp00_07 / 10000 //scale by 10000

		*Censor Sex Ratio
		replace `mm'_isexratio00_07 =1.059   if `mm'_isexratio00_07  < 1.059
		ren	`mm'_isexratio00_07 `mm'_isr00_07

	}
	
*Define parent source country characteristics
	global varlist "lfp fert gdp sr score"

	foreach v of global varlist {
			cap drop igen`v'
			g igen`v' = m_i`v'00_07 if (own_imm_mom == 1) //use mother's country characteristics
			replace igen`v' = f_i`v'00_07 if (own_imm_mom == 0 & own_imm_dad == 1)  //use father's country characteristics if mother not an immigrant
			}

	cap drop ligengdp
	g ligengdp = m_ligdp00_07 if (own_imm_mom == 1)
	replace ligengdp = f_ligdp00_07 if (own_imm_mom == 0 & own_imm_dad == 1) //use father's country characteristics if mother not an immigrant
		
	cap drop ibpld
	g ibpld = m_bpld if (own_imm_mom == 1)
	replace ibpld = f_bpld if (own_imm_mom == 0 & own_imm_dad == 1)

*Spouse second generation indicator
	gen sp_secgen = 0
	replace sp_secgen = 1 if sp_imm_dad==1 | sp_imm_mom==1
	replace sp_secgen = 0 if sp_imm==1
	
*Generate Interactions
	gen girl_lfp = chld1*igenlfp
	gen girl_sr = chld1*igensr
	gen girl_ggi = chld1*igenscore

*Label Variables
	label var igenfert "Total Fertility"
	label var ligengdp "Log of GDP"
	label var igenscore "Equity Index"
	label var chld1 "First Child Girl"
	label var igenlfp "Labor Force Part."
	label var igensr "Sex Ratio at Birth"
	
	label var girl_lfp "Girl*Labor Force Part."
	label var girl_sr "Girl*Sex Ratio at Birth"
	label var girl_ggi "Girl*Equity Index"



/*******************************************************************************

II. Fertility Regressions

*******************************************************************************/

*Column 1 (Original)
local sample "if (own_imm_mom == 1 | own_imm_dad== 1) & citizen!=1 & secgen==1 & gt0chld==1 & core_sampf==1"

local wgt "hhwtnorm"

local controls "lths scol cold sp_lths sp_scol sp_cold" //leave out: hsdi
local controls "`controls' i.genrace i.sp_genrace i.region i.year age age2 age3" 
local controls "`controls' sp_age sp_age2 sp_age3 sp_imm sp_secgen"

local main "igenfert ligengdp igenscore chld1 girl_ggi"

reg nchild `main' `controls' `sample' [aw=`wgt'], cluster(ibpld)
qui sum nchild `sample'  [aw=`wgt']
local dep_mean : display %9.4f r(mean)
outreg2 using "$routput\Table R8.xls", keep(`main') title("Table R8: Effects of Source Country Characteristics on Fertility, Second Generation Sample (At Least One Parent Foreign Born; Table 5 Control for Age at First Birth)" ) ctitle(Orig.) replace addtext(Mean, `dep_mean') label dec(4) nocons nor2

*Column 1 (With AFB Control)
local controls "`controls' age_fb" 

reg nchild `main' `controls' `sample' [aw=`wgt'], cluster(ibpld)
qui sum nchild `sample'  [aw=`wgt']
local dep_mean : display %9.4f r(mean)
outreg2 using "$routput\Table R8.xls", keep(`main') ctitle(AFB Control) append addtext(Mean, `dep_mean') label dec(4) nocons nor2



*Column 2 (Original)
local sample "if (own_imm_mom == 1 | own_imm_dad== 1) & citizen!=1 & secgen==1 & gt0chld==1 & core_sampf==1"

local wgt "hhwtnorm"

local controls "lths scol cold sp_lths sp_scol sp_cold" //leave out: hsdi
local controls "`controls' i.genrace i.sp_genrace i.region i.year age age2 age3" 
local controls "`controls' sp_age sp_age2 sp_age3 sp_imm sp_secgen"

local main "igenfert ligengdp igenlfp chld1 girl_lfp"

reg nchild `main' `controls' `sample' [aw=`wgt'], cluster(ibpld)
qui sum nchild `sample'  [aw=`wgt']
local dep_mean : display %9.4f r(mean)
outreg2 using "$routput\Table R8.xls", keep(`main') ctitle(Orig.) append addtext(Mean, `dep_mean') label dec(4) nocons nor2

*Column 2 (With AFB Control)
local controls "`controls' age_fb" 

reg nchild `main' `controls' `sample' [aw=`wgt'], cluster(ibpld)
qui sum nchild `sample'  [aw=`wgt']
local dep_mean : display %9.4f r(mean)
outreg2 using "$routput\Table R8.xls", keep(`main') ctitle(AFB Control) append addtext(Mean, `dep_mean') label dec(4) nocons nor2



*Column 3 (Original)
local sample "if (own_imm_mom == 1 | own_imm_dad== 1) & citizen!=1 & secgen==1 & gt0chld==1 & core_sampf==1"

local wgt "hhwtnorm"

local controls "lths scol cold sp_lths sp_scol sp_cold" //leave out: hsdi
local controls "`controls' i.genrace i.sp_genrace i.region i.year age age2 age3" 
local controls "`controls' sp_age sp_age2 sp_age3 sp_imm sp_secgen"

local main "igenfert ligengdp igenlfp igensr chld1 girl_lfp girl_sr"

reg nchild `main' `controls' `sample' [aw=`wgt'], cluster(ibpld)
qui sum nchild `sample'  [aw=`wgt']
local dep_mean : display %9.4f r(mean)
outreg2 using "$routput\Table R8.xls", keep(`main') ctitle(Orig.) append addtext(Mean, `dep_mean') label dec(4) nocons nor2


*Column 3 (With AFB Control)
local controls "`controls' age_fb" 

reg nchild `main' `controls' `sample' [aw=`wgt'], cluster(ibpld)
qui sum nchild `sample'  [aw=`wgt']
local dep_mean : display %9.4f r(mean)
outreg2 using "$routput\Table R8.xls", keep(`main') ctitle(AFB Control) append addtext(Mean, `dep_mean') label dec(4) sortvar(igenfert ligengdp igenlfp igensr igenscore chld1 girl_lfp girl_sr girl_ggi) nocons nor2
